package cn.com.pajx.pajx_visitor_v3.utils;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * create by ruancw on 2024/2/28.
 * activity对象管理
 */
public class ActivityManagerUtil {

    private static ActivityManagerUtil mExitUtil;
    // Activity的对象池
    private List<Activity> activityPools;

    public static ActivityManagerUtil getInstant() {
        if (mExitUtil == null)
            mExitUtil = new ActivityManagerUtil();
        return mExitUtil;
    }

    /**
     * 将一个Activity放入对象池
     *
     * @param act activity
     */
    public void addToPool(Activity act) {
        if (activityPools == null)
            activityPools = new ArrayList<>();
        activityPools.add(act);
    }

    public void popPool(Activity act) {
        activityPools.remove(act);
    }

    /**
     * 获取对象池中最后一个activity
     *
     * @return activity
     */
    public Activity getLastAct() {
        if (activityPools != null) {
            return activityPools.get(activityPools.size());
        }
        return null;
    }

    /**
     * 清空对象池
     */
    public void clearPool() {
        if (activityPools == null)
            return;
        activityPools.clear();
    }

    /**
     * 退出程序
     * 清空activity池 在杀死该应用进程
     */
    public void exitApp(Context context) {
        if (activityPools != null) {
            for (int i = 0; i < activityPools.size(); i++) {
                activityPools.get(i).finish();
            }
        }
        System.exit(0);

    }

    public void clearOthers(Context context) {
        if (activityPools != null) {
            for (int i = 0; i < activityPools.size(); i++) {
                if (i != activityPools.size() - 1) {
                    activityPools.get(i).finish();
                }
            }
        }
    }
}
